// pages/qrcode/qrcode.js
var common = require("../../utils/Conmon.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    img: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    common.pageInit(function() {});

    var userInfo = wx.getStorageSync('UserInfo');
    var that = this;
    if (userInfo == "") {
      wx.showModal({
        content: "用户消息获取失败,将跳到登录页",
        showCancel: false,
        confirmText: "确定",
        success: function(res) {
          if (res.confirm) {
            wx.redirectTo({
              url: '/pages/login/login',
            })
            return false;
          } else {
            return false;
          }
        },
        fail: function(res) {
          return false;
        }
      })
    }
    var globalData = getApp().globalData;
    var postData = null;
    var url = "";
    if (globalData.isAuthComponent) {
      url = globalData.apiurl + "/member/api/Small/ComponentCreateCode";
      postData = {
        ZPAppid: globalData.zhengpinAppid,
        custid: globalData.custID,
        parentId: wx.getStorageSync('UserInfo').UserID
      };
    } else {
      url = globalData.apiurl + "/member/api/Small/NewCreateCode";
      postData = {
        custid: globalData.custID,
        secret: globalData.secret,
        appId: globalData.zpAppid,
        parentId: wx.getStorageSync('UserInfo').UserID,
      }
    }
    wx.showLoading({
      title: '名片加载中...',
      mask: true
    });
    wx.request({
      url: url,
      method: "get",
      dataType: "json",
      data: postData,
      success: function(res) {
        wx.hideLoading();
        if (res.data.code == 1200 && res.data.content) {
          var img = res.data.content;
          that.setData({
            img: img,
          })
        } else if (res.statusCode == 500) {
          wx.showModal({
            content: "服务器错误",
            showCancel: false,
            confirmText: "确定"
          })
        } else {
          wx.showModal({
            content: res.data.msg,
            showCancel: false,
            confirmText: "确定"
          })
        }
      },
      fail: function(res) {
        wx.showModal({
          content: "网络请求失败！",
          showCancel: false,
          confirmText: "确定"
        })
      }
    });

  },
  saveImg: function() {
    var that = this;
    wx.previewImage({
      urls: [that.data.img],
      success: res => {
        console.log(res)
      },
      fail: res => {
        console.log(res);
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})